#include "client_manager.h"
#include "task_runnable.h"

ClientManager * ClientManager::m_instance = nullptr;
QMutex ClientManager::m_mutex;

ClientManager & ClientManager::getInstance()
{
    if(nullptr == m_instance){
        QMutexLocker lock(&m_mutex);
        if(nullptr == m_instance){
            m_instance = new ClientManager();
        }
    }

    return * m_instance;
}

void ClientManager::addTask(TaskRunnable *task)
{
    m_clientList.push_back(task);
}

void ClientManager::removeOne(TaskRunnable *task)
{
    m_clientList.removeOne(task);
}

void ClientManager::sendData(const QByteArray &arr)
{
    //分别调用每个线程的发送
    foreach(TaskRunnable *t  , m_clientList)
    {
        t->sendData(arr);
    }
}

int ClientManager::clientCount()
{
    return m_clientList.size();
}

void ClientManager::closeAllClients()
{
    //分别调用每个线程对象的close
    foreach(TaskRunnable *t  , m_clientList)
    {
        t->close();
    }
    //清空客户端列表
    m_clientList.clear();
}
